<template>
  <view class="dashboard-container">
    <view class="dashboard-tabs">
      <view 
        class="tab-item" 
        :class="{ 'active': activeTab === '本日业绩' }"
        @click="activeTab = '本日业绩'"
      >本日业绩</view>
      <view 
        class="tab-item" 
        :class="{ 'active': activeTab === '本月业绩' }"
        @click="activeTab = '本月业绩'"
      >本月业绩</view>
    </view>

    <view class="performance-summary">
      <view class="performance-item">
        <text class="performance-number">{{ activeTab === '本日业绩' ? todayTotalOrders : monthTotalOrders }}</text>
        <text class="performance-label">总配送</text>
      </view>
      <view class="performance-item">
        <text class="performance-number">{{ activeTab === '本日业绩' ? todayOnTimeOrders : monthOnTimeOrders }}</text>
        <text class="performance-label">准时送达</text>
      </view>
      <view class="performance-item">
        <text class="performance-number">{{ activeTab === '本日业绩' ? todayDelayedOrders : monthDelayedOrders }}</text>
        <text class="performance-label">超时送达</text>
      </view>
    </view>
  </view>
</template>

<script>
import { request } from '@/utils/request'

export default {
  data() {
    return {
      activeTab: '本日业绩',
      todayTotalOrders: 0,
      todayOnTimeOrders: 0,
      todayDelayedOrders: 0,
      monthTotalOrders: 0,
      monthOnTimeOrders: 0,
      monthDelayedOrders: 0
    }
  },
  onShow() {
    this.fetchDashboardData()
  },
  watch: {
    activeTab(val) {
      if (val === '本日业绩') {
        this.fetchDashboardData()
      } else if (val === '本月业绩') {
        this.fetchMonthDashboardData()
      }
    }
  },
  methods: {
    async fetchDashboardData() {
      try {
        const res = await request({
          url: '/courier/dashboardDay',
          method: 'GET',
          data: {
            courierAccount: uni.getStorageSync('courierAccount')
          }
        })
        if (res.code === 200 && Array.isArray(res.data) && res.data.length > 0) {
          const stat = res.data[0]
          this.todayTotalOrders = stat.totalOrders
          this.todayOnTimeOrders = stat.onTimeOrders
          this.todayDelayedOrders = stat.delayedOrders
        }
      } catch (err) {
        console.error('获取业绩看板数据失败:', err)
        uni.showToast({
          title: '获取数据失败',
          icon: 'none'
        })
      }
    },
    async fetchMonthDashboardData() {
      try {
        const res = await request({
          url: '/courier/dashboardMonth',
          method: 'GET',
          data: {
            courierAccount: uni.getStorageSync('courierAccount')
          }
        })
        if (res.code === 200 && Array.isArray(res.data) && res.data.length > 0) {
          const stat = res.data[0]
          this.monthTotalOrders = stat.totalOrders
          this.monthOnTimeOrders = stat.onTimeOrders
          this.monthDelayedOrders = stat.delayedOrders
        }
      } catch (err) {
        console.error('获取本月业绩数据失败:', err)
        uni.showToast({
          title: '获取数据失败',
          icon: 'none'
        })
      }
    },
    getOrderStatusText(status) {
      switch(status) {
        case 0: return '配送中'
        case 1: return '准时送达'
        case 2: return '超时送达'
        default: return '未知状态'
      }
    }
  }
}
</script>

<style scoped>
.dashboard-container {
  background-color: #f4f4f4;
  height: 100vh;
}

.dashboard-tabs {
  display: flex;
  background-color: white;
  border-bottom: 1px solid #e0e0e0;
}

.tab-item {
  flex: 1;
  text-align: center;
  padding: 15px 0;
  color: #666;
  position: relative;
}

.tab-item.active {
  color: #007AFF;
  font-weight: bold;
}

.tab-item.active::after {
  content: '';
  position: absolute;
  bottom: 0;
  left: 50%;
  transform: translateX(-50%);
  width: 40px;
  height: 3px;
  background-color: #007AFF;
}

.performance-summary {
  display: flex;
  background-color: white;
  padding: 20px 0;
}

.performance-item {
  flex: 1;
  display: flex;
  flex-direction: column;
  align-items: center;
}

.performance-number {
  font-size: 24px;
  font-weight: bold;
  color: #333;
}

.performance-label {
  font-size: 14px;
  color: #666;
  margin-top: 5px;
}
</style> 